package com.baidu.travel.service.impl;

import com.baidu.travel.dao.LogtableDao;
import com.baidu.travel.domain.Logtable;
import com.baidu.travel.domain.LogtablePO;
import com.baidu.travel.domain.QueryCondition;
import com.baidu.travel.service.LogtableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LogtableServiceImpl implements LogtableService {
    @Autowired
    private LogtableDao logtableDao;
    @Override
    public boolean addLog(Logtable log) throws SQLException {
        return logtableDao.insert(log) > 0 ? true : false;
    }

    @Override
    public List<LogtablePO> getByPage(QueryCondition queryCondition) {
        int pageNo=queryCondition.getPageNo();
        int pageSize=queryCondition.getPageSize();
        queryCondition.setPageNo((pageNo-1)*pageSize);
        /**
         * 获取分页显示的数据
         */
        List<Logtable> logtableList=logtableDao.getByPage(queryCondition);
        List<LogtablePO> logtablePOS = new ArrayList<>();
        LogtablePO logtablePO = new LogtablePO();
        /**
         * 用户数据
         */
        logtablePO.setData(logtableList);
        /**
         * 当前页码
         */
        logtablePO.setCurrentPage(pageNo);

        /**
         * 总记录数
         */
        Integer count = logtableDao.getCount(queryCondition);
        logtablePO.setTotalRecord(count);
        /**
         * 总页数
         */
        Integer totalPage = (count % pageSize== 0) ? (count/pageSize) : ((count/pageSize) + 1);
        logtablePO.setTotalPage(totalPage);
        logtablePOS.add(logtablePO);
        return logtablePOS;
    }

}